package main

import "fmt"

//i只在循环体内有效
func testFor1() {
  for i := 1; i <= 10; i++ {
    fmt.Printf("i=%d\n", i)
  }

  //print(i) //Unresolved reference 'i'
}

//break
func testFor2() {
  for i := 1; i <= 10; i++ {
    if i > 5 {
      break
    }

    fmt.Printf("i=%d\n", i)
  }
}

//continue
func testFor3() {
  for i := 1; i <= 10; i++ {
    if i%2 == 0 {
      continue
    }

    fmt.Printf("i=%d\n", i)
  }
}

//省略
func testFor4() {
  i := 1
  for ; /*←这个分号其实也可以省略*/ i <= 10; /*←这个分号其实也可以省略*/ {
    fmt.Printf("i=%d\n", i)
    i = i + 2
  }
}


func testFor5() {
  //var a int
  //var b string
  //var c int
  //a, b, c = 10, "hello", 100
  a, b, c := 10, "hello", 100

  fmt.Printf("a=%v b=%v c=%v\n", a, b, c) //a=10 b=hello c=100
}
func testFor6() {
  for no, i := 10, 1; i <= 10 && no <= 19; i, no = i+1, no+1 {
    fmt.Printf("%d*%d=%d\n", no, i, no*i)
  }
}


//无限循环
//go 里循环貌似就只有 for
//So 只能如此实现 while(true)的效果
func testFor7(){
  for {
    fmt.Printf("hello\n")
  }
}

//for initialisation; condition; post{}
func main() {
  //testFor1()

  //testFor2()

  //testFor3()

  //testFor4()

  //testFor5()

  //testFor6()

  testFor7()
}
